<template>
  <div class="container">
    <el-form
      :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <div class="head">
        <el-form-item label="菜品名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="菜品分类" prop="categoryId">
          <el-select v-model="ruleForm.categoryId" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </div>

      <el-form-item label="菜品价格" prop="price">
        <el-input v-model="dishPrice"></el-input>
      </el-form-item>
      <el-form-item label="口味做法配置" prop="flavors">
        <el-input v-model="ruleForm.flavors"></el-input>
      </el-form-item>
      <el-form-item label="菜品图片" prop="image">
        <el-upload
          class="avatar-uploader"
          :action="`${env}/common/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img
            v-if="ruleForm.image"
            :src="`${env}/common/download?name=${ruleForm.image}`"
            class="avatar"
          />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>

      <el-form-item label="菜品描述" prop="description">
        <el-input type="textarea" v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >保存</el-button
        >
        <el-button @click="$router.push({ name: 'Course' })">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  addCourse,
  changeStatus,
  getCourseById,
  getDishes,
  upload,
} from "@/api/course";
export default {
  data() {
    return {
      env: process.env.VUE_APP_BASE_API,
      options: [],
      dialogVisible: false,
      disabled: false,
      ruleForm: {
        id: "",
        name: "",
        categoryId: "",
        price: "",
        code: "",
        image: "",
        description: "",
        flavors: [],
      },
    };
  },
  created() {
    getDishes().then((res) => {
      this.options = res.data;
    });
    if (this.isEdit) {
      getCourseById(this.id).then((res) => {
        this.ruleForm = res.data;
      });
    }
  },
  computed: {
    dishPrice: {
      get() {
        return this.ruleForm.price ? this.ruleForm.price / 100 : "";
      },
      set(value) {
        this.ruleForm.price = Math.round(value * 100);
      },
    },
    isEdit() {
      return this.$route.query.isEdit === "1";
    },
    id() {
      return this.$route.query.id;
    },
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          if (this.isEdit) {
            await changeStatus(this.ruleForm);
            this.$message.success("编辑成功");
          } else {
            await addCourse(this.ruleForm);
            this.$message.success("添加成功");
          }
          this.$router.push({ name: "Course" });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 上传
    handleAvatarSuccess(res) {
      this.ruleForm.image = res.data;
      this.$message.success("图片上传成功");
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 50px;

  .demo-ruleForm {
    width: 400px;
  }
}

.head {
  width: 800px;
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}
</style>
<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
